package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.NumberFormatUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.TimeUtils;
import com.yidiandao.business.utils.UIUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 活动项子page的provider
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/4/12 14:59
 */
public class DiscoveryActivePageProvider extends ItemViewProvider<Article, DiscoveryActivePageProvider.ViewHolder> {
    private Activity mActivity;

    public DiscoveryActivePageProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_discovery_active, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final Article article) {
        //图片
        ImageLoader.loadBanner(mActivity, holder.mIvPic, article.getArticleThumb());
        //标题
        holder.tvTitle.setText(article.getArticleTitle());
        //描述
        holder.tvContent.setText(article.getArticleDesc());

        int color = article.getIsEnd() == 0 ? 0xff484848 : 0xff808080;//0 未结束
        holder.tvTitle.setTextColor(color);
        holder.tvContent.setTextColor(color);
        holder.rlMask.setVisibility(article.getIsEnd() == 0 ? View.GONE : View.VISIBLE);
        //时间
        String time = TimeUtils.getRelativeTimeWithSingleUnit(mActivity.getResources(), TimeUtils.getAndroidMillis(article.getAddTime()), true);
        holder.tvTime.setText(time);
        //阅读数
        holder.tvRead.setText(UIUtils.getString(R.string.discovery_has_read, NumberFormatUtils.getLargeFormatValue(article.getWatchCount())));
        //文章id
        final String articleId = article.getArticleId();
        holder.rootItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UrlActivity.actionStartForArticle(mActivity, articleId);
            }
        });

        // 活动费用
        showActiveMoney(holder, article);
    }

    private void showActiveMoney(@NonNull ViewHolder holder, @NonNull Article article) {
        String enrollMoneyStr = article.getEnrollMoney();

        if (TextUtils.isEmpty(enrollMoneyStr)) {
            holder.tvActiveMoney.setVisibility(View.GONE);
            return;
        }

        holder.tvActiveMoney.setVisibility(View.VISIBLE);

        float enrollMoney;
        try {
            enrollMoney = Float.parseFloat(enrollMoneyStr);
        } catch (NumberFormatException e) {
            enrollMoney = 0;
        }

        SpannableString activeMoneyStyle = TextRenderUtils.getAppearanceText(UIUtils.getString(R.string.activity_money), R.style.ActivityMoneyStyle);

        SpannableString beautyPrice = null;
        if (enrollMoney == 0) {
            beautyPrice = TextRenderUtils.getAppearanceText(UIUtils.getString(R.string.free), R.style.ActivityFreeStyle);
        } else {
            beautyPrice = TextRenderUtils.getBeautyPrice(enrollMoney + "", null, UIUtils.dp2px(20), UIUtils.dp2px(12));
        }
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(activeMoneyStyle);
        builder.append(beautyPrice);

        holder.tvActiveMoney.setText(builder);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvPic;
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvTime;
        private TextView tvRead;
        private LinearLayout rootItem;
        private RelativeLayout rlMask;
        private TextView tvActiveMoney;

        private ViewHolder(View view) {
            super(view);
            rlMask = (RelativeLayout) view.findViewById(R.id.rl_mask);
            rootItem = (LinearLayout) view.findViewById(R.id.root_item);
            mIvPic = (ImageView) view.findViewById(R.id.iv_pic);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvRead = (TextView) view.findViewById(R.id.tv_read);
            tvActiveMoney = (TextView) view.findViewById(R.id.tv_active_money);
        }
    }
}
